package com.bit.demo.controller;

import com.bit.demo.model.BookInfo;
import com.bit.demo.model.PageRequest;
import com.bit.demo.model.PageResult;
import com.bit.demo.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    @RequestMapping("/addBook")
    public String addBook(BookInfo bookInfo) {
        log.info("添加图书: {}" + bookInfo);
        if (   !StringUtils.hasLength(bookInfo.getBookName())
            || !StringUtils.hasLength(bookInfo.getAuthor())
            || bookInfo.getCount() == null
            || bookInfo.getPrice() == null
            || !StringUtils.hasLength(bookInfo.getPublish())
            || bookInfo.getStatus() == null
        ) {
            return "参数不合法，重新输入 !";
        }

        try{
            bookService.addBook(bookInfo);
            return "";
        } catch (Exception e) {
            log.error("添加图书异常: {}" + e);
            return "添加图书异常: " + e.getMessage();
        }
    }

    @RequestMapping("/getListByPage")
    public PageResult<BookInfo> getListByPage(PageRequest pageRequest) {
        log.info("查询图书列表: {}" + pageRequest);
        return bookService.getListByPage(pageRequest);
    }

    @RequestMapping("queryBookById")
    public BookInfo queryBookById(Integer id) {
        log.info("查询图书: {}" + id);
        return bookService.queryBookById(id);
    }

    @RequestMapping("updateBook")
    public String updateBook(BookInfo bookInfo) {
        log.info("更新图书: {}" + bookInfo);
        try{
            bookService.updateBook(bookInfo);
            return "";
        } catch (Exception e) {
            log.error("更新图书异常: {}" + e);
            return "更新图书异常: " + e.getMessage();
        }
    }

    @RequestMapping("/deleteBook")
    public String deleteBook(Integer id) {
        log.info("删除图书: {}" + id);
        try{
            Integer result = bookService.deleteBook(id);
            if (result < 1) {
                return "图书删除失败，请联系管理员";
            }
            return "";
        } catch (Exception e) {
            log.error("删除图书异常: {}" + e);
            return "删除图书异常: " + e.getMessage();
        }
    }

    @RequestMapping("/batchDeleteBook")
    public String batchDeleteBook(@RequestParam List<Integer> ids) {
        log.info("批量删除图书: {}" + ids);
        try{
            bookService.batchDeleteBook(ids);
            return "";
        } catch (Exception e) {
            log.error("批量删除图书异常: {}" + e);
            return "批量删除图书异常: " + e.getMessage();
        }
    }

}
